<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
        ref="formRef"
        v-loading="formLoading"
        :model="formData"
        :rules="formRules"
        label-width="100px"
    >
      <el-form-item label="设备型号" prop="code">
        <el-input v-model="formData.code" placeholder="请输入设备型号"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入设备名称"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input v-model="formData.brand" placeholder="请输入品牌"/>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="formData.price" placeholder="请输入价格"/>
      </el-form-item>
      <el-form-item label="数量" prop="mount">
        <el-input v-model="formData.mount" placeholder="请输入数量"/>
      </el-form-item>
      <el-form-item label="生产日期" prop="productTime">
        <el-date-picker
            v-model="formData.productTime"
            type="date"
            placeholder="请选择生产日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="生产厂商" prop="productFactory">
        <el-input v-model="formData.productFactory" placeholder="请输入生产厂商"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import * as RoleApi from '@/api/system/role'
import * as ProjectApi from '@/api/laboratory/report'

// 日期选择
var productTime = ref('')


defineOptions({name: 'SystemRoleForm'})

const {t} = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  code: "", //设备型号
  name: "", //设备名称
  brand: "", // 品牌
  price: "", //价格
  mount: "", //数量
  productTime: "", //生产日期
  productFactory: "", //生产厂商
})
const formRules: any = reactive({
  code: [{required: true, message: '设备型号不能为空', trigger: 'blur'}],
  name: [{required: true, message: '设备名称不能为空', trigger: 'change'}],
  brand: [{required: true, message: '品牌不能为空', trigger: 'change'}],
  price: [{required: true, message: '价格不能为空', trigger: 'change'}],
  mount: [{required: true, message: '数量不能为空', trigger: 'blur'}],
  productTime: [{required: true, message: '生产日期不能为空', trigger: 'blur'}],
  productFactory: [{required: true, message: '生产厂商不能为空', trigger: 'blur'}],
})
const formRef = ref() // 表单 Ref




/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type

  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      const data = await ProjectApi.getEquipmentById(id)
      console.log(data)
      formData.value = data.result
    } finally {
      formLoading.value = false
    }
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    code: "", //设备型号
    name: "", //设备名称
    brand: "", // 品牌
    price: "", //价格
    mount: "", //数量
    productTime: "", //生产日期
    productFactory: "", //生产厂商
  }
  formRef.value?.resetFields()
}
defineExpose({open}) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as RoleApi.RoleVO
    if (formType.value === 'create') {
      await ProjectApi.AddEquipment(data)
      message.success(t('common.createSuccess'))
    } else {
      await ProjectApi.updateEquipment(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>
